﻿#ifndef PRODUCT_H
#define PRODUCT_H

#include <QObject>
#include <QMetaType>
#include "tools.h"
#include <QJsonArray>
#include <QSqlQuery>

class Product : public QObject
{
    Q_OBJECT

public:
    explicit Product(QObject *parent = nullptr);

    enum SanBaoMoveType{
        SanBaoMoveOut=0,    //出库 三包库-->库存
        SanBaoMoveIn=1      //入库 库存-->三包库
    };

    Q_ENUM(SanBaoMoveType)

    int id;
    QString productId;
    QString companyId;

    QString type;
    QString typePinyin;
    QString name;
    QString namePinyin;
    QString unit;
    QString productNum;
    int upperLimit;
    int lowerLimit;

    QString brand;
    QString originPlace;
    QString specifications;
    QString barCode;
    double inPrice;
    QString remark;

    double guidePrice1;
    double guidePrice2;

    QString primaryDirectory;
    QString secondaryDirectory;

    int count;              //库存数量
    int countNum;           //盘点数量
    int planCount;          //计划数量
    int sanbao;             //三包库数量
    int sanbaoMoveCount;    //三包库移库数量

    Product(QJsonObject obj);

    void loadFromJsonObject(QJsonObject obj);

    void loadFromSqlQuery(QSqlQuery query);

    QString toJson();

    //检查当前产品是否在输入产品列表中
    bool checkExists(QList<Product*> products);

    static QString toJson(QList<Product*> products){
        QString jsonStr = "[";

        for(Product *product:products){
            jsonStr.append(product->toJson()+",");
        }

        jsonStr.append("]");

        return jsonStr;
    }

    static QList<bool> getHiddenStateList(QString key){
        QString hiddenStateStr = MySettings.value("product/"+key).toString();
        QStringList hiddenState = hiddenStateStr.split("#");
        QList<bool> hiddenStateList;

        for(QString curState:hiddenState){
            if(curState == ""){
                break;
            }
            hiddenStateList<<curState.toInt();
        }

        return hiddenStateList;
    }

    static void setHiddenStateList(QString key, QList<bool> hiddenStateList){
        QString hiddenStateStr;

        for(int curState:hiddenStateList){
            hiddenStateStr.append(QString("%1#").arg(curState));
        }

        MySettings.setValue("product/"+key, hiddenStateStr);
    }
signals:

public slots:
};

#endif // PRODUCT_H
